/*
 * 创建日期 2005-10-5
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import java.util.List;

import org.apache.commons.validator.GenericValidator;
import org.apache.poi.hssf.usermodel.HSSFRow;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportGroupRow;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.BeanElement;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class RowElement extends BeanElement {
	private int format_row;
	private int start = -1;
	private int end = -1;
	private String start_col;

	private boolean wrap = false;
	
	public void validator() throws ReportException {
		//Validation.checkRequired("RowElement", "format_row", format_row, 0);
	}

	/**
	 * 标签执行方法，递归执行
	 * temp2:当前行的汇率
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException {

		try {
			ReportInnerData reportData = (ReportInnerData) obj;
			String name = super.getName();
			String property = super.getProperty();

			Object rowData = null;
			Object contextObj = super.getNameObject(reportData);

			//初始化临时变量
			reportData.setTemp2(null);

			//如果为null，则从ContextObject取数据
			if (GenericValidator.isBlankOrNull(property)) {
				rowData = contextObj;
			} else {
				//表示不是数组
				if (start == -1 && end == -1) {
					rowData = super.getProperty(contextObj, property);
				} else {
					if (contextObj instanceof List) {
						if (start >= 0) {
							rowData = ((List) contextObj).get(start);
						} else {
							rowData =
								((List) contextObj).get(
									((List) contextObj).size() - end - 1);
						}
					}

				}
			}
			// 只处理非统计行的数据
			if (rowData instanceof ReportGroupRow)
				return;

			reportData.setContextData(rowData);
			reportData.setFormatRowNum(format_row);
			//如果设置了fomat_row,则将format_row拷贝过来,并删除拷贝行上的数据
			if (format_row > 0) {
				//拷贝format_row,但不拷贝内容
				ExcelUtils.copyRows(
					reportData.getFormatSheet(),
					reportData.getReportSheet(),
					format_row-1,
					format_row-1,
					reportData.getCurrentRow(),
					false,
					wrap);
			}

			if (start_col != null) {
				int startCol = ExcelUtils.getColIndex(start_col);
				if (startCol > 0) {
					reportData.setStart_col(startCol);
				}
			}

			//设置currentCol
			if (reportData.getStart_col() <= 0) {
				reportData.setCurrentCol(0);
			} else {
				reportData.setCurrentCol(reportData.getStart_col() - 1);
			}

			super.execute(obj);

			reportData.setCurrentCol(0);
			//行数＋1
			reportData.setCurrentRow(reportData.getCurrentRow() + 1);
			//清除临时变量
			reportData.setTemp2(null);
			//System.out.println("row currentRow = "+reportData.getCurrentRow());
		} catch (ReportException e) {
			throw e;
		} catch (Exception e) {
			throw new ReportException(e);
		}

	}

	/**
	 * @return
	 */
	public int getEnd() {
		return end;
	}

	/**
	 * @return
	 */
	public int getFormat_row() {
		return format_row;
	}

	/**
	 * @return
	 */
	public int getStart() {
		return start;
	}

	/**
	 * @param i
	 */
	public void setEnd(int i) {
		end = i;
	}

	/**
	 * @param i
	 */
	public void setFormat_row(int i) {
		format_row = i;
	}

	/**
	 * @param i
	 */
	public void setStart(int i) {
		start = i;
	}

	/**
	 * @return
	 */
	public String getStart_col() {
		return start_col;
	}

	/**
	 * @param string
	 */
	public void setStart_col(String string) {
		start_col = string;
	}

	/**
	 * @return
	 */
	public boolean isWrap() {
		return wrap;
	}

	/**
	 * @param b
	 */
	public void setWrap(boolean b) {
		wrap = b;
	}

}
